﻿using DeviceFaultPushService.MessagePushService.Models;
using DeviceFaultPushService.SystemService;

namespace DeviceFaultPushService.MessagePushService;

/// <summary>
/// 消息推送
/// </summary>
public class MessagePush
{
    /// <summary>
    /// 日志委托
    /// </summary>
    public delegate void LogDelegate(string log);
    /// <summary>
    /// 日志事件
    /// </summary>
    public event LogDelegate LogEvent;
    /// <summary>
    /// 日志
    /// </summary>
    /// <param name="log"></param>
    private void Log(string log)
    {
        if (LogEvent is not null)
        {
            LogEvent(log);
        }
    }

    /// <summary>
    /// 消息接收服务
    /// </summary>
    /// <param name="contacts">联系人</param>
    /// <param name="name">设备名称</param>
    /// <param name="information">信息</param>
    /// <param name="level">等级</param>
    public void Receive(List<ContactsModel> contacts, string name, string information, UserLevelEnum level = UserLevelEnum.User)
    {
        //获取模板
        string template = "";
        template = GetConfiguration.Configuration.Template;
        template = template.Replace("name", name);
        template = template.Replace("date", DateTime.Now.ToString("F"));
        template = template.Replace("information", information);
        template = template.Replace("level", level.ToString());

        Log(template);
        if (GetConfiguration.Configuration.EnableVoiceBroadcastingService)
        {
            PlayService.Add(template);
        }
        //确认推送服务开启
        if (GetConfiguration.Configuration.EnablePushService)
        {
            SendMSM(contacts, template, level);
            SensCall(contacts, template, level);
        }
    }

    /// <summary>
    /// 短信发送
    /// </summary>
    /// <param name="contacts"></param>
    /// <param name="content"></param>
    /// <param name="level"></param>
    private void SendMSM(List<ContactsModel> contacts, string content, UserLevelEnum level)
    {
        List<string> contactsList = new();
        //联系人合并
        if (contacts != null && contacts.Count > 1)
        {
            foreach (var itme in contacts)
            {
                if (itme.EnableSms)
                {
                    contactsList.Add(itme.PhoneNumber);
                }
            }
        }
        //全局联系人
        if (GetConfiguration.Configuration.Contacts is not null && GetConfiguration.Configuration.Contacts.Count >= 1)
        {
            foreach (var itme in contacts)
            {
                if ((int)itme.UserLevel >= (int)level && itme.EnableSms)
                {
                    contactsList.Add(itme.PhoneNumber);
                }
            }
        }

        PushService.Add(MessageTypeEnum.Sms, contactsList, content);
    }

    private void SensCall(List<ContactsModel> contacts, string content, UserLevelEnum level)
    {
        List<string> contactsList = new();
        //联系人合并
        if (contacts != null && contacts.Count > 1)
        {
            foreach (var itme in contacts)
            {
                if (itme.EnableDialing)
                {
                    contactsList.Add(itme.PhoneNumber);
                }
            }
        }
        //全局联系人
        if (GetConfiguration.Configuration.Contacts is not null && GetConfiguration.Configuration.Contacts.Count >= 1)
        {
            foreach (var itme in contacts)
            {
                if ((int)itme.UserLevel >= (int)level && itme.EnableDialing)
                {
                    contactsList.Add(itme.PhoneNumber);
                }
            }
        }

        PushService.Add(MessageTypeEnum.Call, contactsList, content);
    }
}